package com.atguigu.webadmin.controller.user;


import com.atguigu.common.result.Result;
import com.atguigu.model.pojo.UserInfo;
import com.atguigu.model.enums.BaseStatus;
import com.atguigu.webadmin.service.UserInfoService;
import com.atguigu.webadmin.vo.user.UserInfoQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "用户信息管理")
@RestController
@RequestMapping("/admin/user")
@CrossOrigin
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Operation(summary = "分页查询用户信息")
    @Parameters({
            @Parameter(name = "current",description = "当前页码",in = ParameterIn.DEFAULT),
            @Parameter(name = "size",description = "页面展示数量",in = ParameterIn.DEFAULT),
            @Parameter(name = "query",description = "用户信息vo对象",in = ParameterIn.DEFAULT)
    })
    @GetMapping("page")
    public Result<IPage<UserInfo>> pageUserInfo(@RequestParam long current,
                                                @RequestParam long size, UserInfoQueryVo queryVo) {
        //封装分页查询条件
        IPage<UserInfo> page = new Page<>(current, size);
        //创建分页查询包装类
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        //指定模糊查询条件
        queryWrapper.like(queryVo.getPhone() != null, UserInfo::getPhone, queryVo.getPhone());
        //指定查询状态
        queryWrapper.eq(queryVo.getStatus() != null, UserInfo::getStatus, queryVo.getStatus());
        //执行查询
        IPage<UserInfo> list = userInfoService.page(page, queryWrapper);
        //返回查询结果
        return Result.ok(list);
    }

    @Operation(summary = "根据用户id更新账号状态")
    @Parameters({
            @Parameter(name = "id",description = "用户id",in = ParameterIn.DEFAULT),
            @Parameter(name = "status",description = "用户状态",in = ParameterIn.DEFAULT)
    })
    @PostMapping("updateStatusById")
    public Result<Boolean> updateStatusById(@RequestParam Long id, @RequestParam BaseStatus status) {
        //创建Lambda更新包装类
        LambdaUpdateWrapper<UserInfo> updateWrapper = new LambdaUpdateWrapper<>();
        //指定查询条件
        updateWrapper.eq(UserInfo::getId, id);
        //设置查询的帐号状态
        updateWrapper.set(UserInfo::getStatus, status);
        //更新帐号状态
        boolean update = userInfoService.update(updateWrapper);
        //返回更新结果
        return Result.ok(update);
    }
}
